package com.cf.conf.controller;

import com.cf.conf.core.UpdatePusher;
import com.cf.conf.core.model.Config;
import com.cf.conf.core.model.ConfigService;
import com.cf.constant.EnumReturnCode;
import com.cf.controller.CommonController;
import com.cf.exception.BizException;
import com.cf.vo.ResponseVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;

/**
 * game-server-ConfigController
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/6/8 下午 05:37
 */

@RequestMapping("/config")
public class ConfigController extends CommonController<Config, ConfigService> {

    @Autowired
    private UpdatePusher updatePusher;

    @Override
    @ResponseBody
    @Transactional
    @RequestMapping(method = {RequestMethod.POST})
    public ResponseVo<Config> create(@RequestBody Config entity) throws BizException {
        ResponseVo<Config> configResponseVo = super.create(entity);
        try {
            updatePusher.push(entity);
        } catch (IOException e) {
            throw new BizException(EnumReturnCode.ERROR.getCode(), "提交属性刷新异常");
        }
        return configResponseVo;
    }

    @Override
    @ResponseBody
    @Transactional
    @RequestMapping(method = {RequestMethod.PUT})
    public ResponseVo<PageInfo<Config>> update(@RequestBody Config entity) {
        ResponseVo<PageInfo<Config>> update = super.update(entity);
        try {
            updatePusher.push(entity);
        } catch (IOException e) {
            throw new BizException(EnumReturnCode.ERROR.getCode(), "提交属性刷新异常");
        }
        return update;
    }

    @Override
    protected String getPageDir() {
        return "config";
    }
}
